package com.sky.service.impl;


import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 获取营业数据
     * @return com.sky.vo.BusinessDataVO
     * @author zhupp
     * @create 2025/3/1
     **/
    @Override
    public BusinessDataVO getBusinessData() {
        BusinessDataVO businessDataVO = new BusinessDataVO(0.0,0,0.0,0.0,0);

        // 今日时间
        LocalDate today = LocalDate.now();
        // 营业额
        Double turnover = Optional.ofNullable(orderMapper.getTodayTurnover(today)).orElse(0.0);

        // 已完成订单数、
        List<Integer> completedOrder = List.of(5);
        Integer completedOrderCount = Optional.ofNullable(orderMapper.selectCountByStatus(completedOrder,today)).orElse(0);

        // 有效订单数
        List<Integer> validOrder = List.of(3, 4, 5);
        Integer validOrderCount = orderMapper.selectCountByStatus(validOrder,today);

        // 订单完成率
        Double orderCompletionRate = validOrderCount != 0 ? completedOrderCount / validOrderCount : 0.0;

        // 平均客单价 营业额/有效订单数
        Double unitPrice = Optional.of(completedOrderCount)
        .filter(count -> count != 0) // 确保completedOrderCount不为0
        .map(count -> turnover / count) // 执行除法操作
        .orElse(0.0); // 如果completedOrderCount为0，则返回默认值0.0
        BigDecimal bd = new BigDecimal(unitPrice);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        // 新增用户数
        List<UserVO> userVOList = userMapper.getNewUserCount();
        userVOList.forEach(userVO -> {
            if (userVO.getDate().equals(today)){
               businessDataVO.setNewUsers(userVO.getCount());
            }
        });
        businessDataVO.setUnitPrice(bd.doubleValue());
        businessDataVO.setTurnover(turnover);
        businessDataVO.setOrderCompletionRate(orderCompletionRate);
        businessDataVO.setValidOrderCount(validOrderCount);
        return businessDataVO;
    }

    /**
     * 查询订单管理数据
     * @return com.sky.vo.OrderOverViewVO
     * @author zhupp
     * @create 2025/3/1
     **/
    @Override
    public OrderOverViewVO getOrderOverView() {
        LocalDate today = LocalDate.now();
        // 全部订单
        Integer allOrders = orderMapper.getByStatusToday(today,null);
        // 已取消数量
        Integer cancelledOrders = orderMapper.getByStatusToday(today, Orders.CANCELLED);
        // 已完成数量
        Integer completedOrders = orderMapper.getByStatusToday(today,Orders.COMPLETED);
        // 待派送数量
        Integer deliveredOrders = orderMapper.getByStatusToday(today,Orders.CONFIRMED);
        // 待接单数量
        Integer waitingOrders = orderMapper.getByStatusToday(today,Orders.TO_BE_CONFIRMED);

        return OrderOverViewVO.builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }

    /**
     * 查询套餐总览
     * @return com.sky.vo.SetmealOverViewVO
     * @author zhupp
     * @create 2025/3/1
     **/
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {

        return SetmealOverViewVO.builder()
                .discontinued(setmealMapper.getSetmealsByStatus(0))
                .sold(setmealMapper.getSetmealsByStatus(1))
                .build();
    }

    /**
     * 查询菜品总览
     * @return com.sky.vo.DishOverViewVO
     * @author zhupp
     * @create 2025/3/1
     **/
    @Override
    public DishOverViewVO getDishOverViewVO() {
        return DishOverViewVO.builder()
                .discontinued(dishMapper.getDishByStatus(0))
                .sold(dishMapper.getDishByStatus(1))
                .build();
    }
}
